package fr.expdev.bndd.widget;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.ViewGroup;
import fr.expdev.bndd.R;
import fr.expdev.bndd.action.Action;
import greendroid.widget.item.SubtitleItem;
import greendroid.widget.itemview.ItemView;

public class CheckItem extends SubtitleItem {
	
	/**
	 * Défini si la case est cochée ou non
	 */
	protected boolean checked;
	
	/**
	 * Action à exécuter lors du click
	 */
	protected Action clickAction;

	public CheckItem() {
		super();
	}

	public CheckItem(String text, String subtitle, boolean checked, Action action) {
		super(text, subtitle);
		this.checked = checked;
		this.clickAction = action;
	}

	public CheckItem(String text, boolean checked, Action action) {
		super(text, null);
		this.checked = checked;
		this.clickAction = action;
	}
	
	/**
	 * Méthode que la vue appelle quand le Check a changé
	 * @param isChecked
	 */
	public void notifyValueChange(boolean isChecked) {
		if (clickAction != null) {
			checked = isChecked;
			clickAction.execute(isChecked);
		}
	}

	@Override
	public ItemView newView(Context context, ViewGroup parent) {
		return createCellFromXml(context, R.layout.check_item_view, parent);
	}

    @Override
    public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException,
            IOException {
        super.inflate(r, parser, attrs);

        TypedArray a = r.obtainAttributes(attrs, R.styleable.CheckItem);
        checked = a.getBoolean(R.styleable.CheckItem_checked, true);
        a.recycle();
    }

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}
	
	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

}
